package com.jietong.rfid.uhf.dao.impl;

import java.util.Map;
import com.jietong.rfid.uhf.callback.CallBack;
import com.jietong.rfid.uhf.dao.R2000ReaderDao;
import com.jietong.rfid.util.AntStruct;
import com.jietong.rfid.util.FrequencyPoint;
import com.jietong.rfid.util.RFIDException;

public class R2000ReaderDaoImpl implements R2000ReaderDao {
	
	@Override
	public R2000Reader serialPortConnect(String portName, int baudRate,CallBack.R2000 callBack) {
		R2000Reader reader = new R2000Reader();
		reader.setHost(portName, baudRate);
		if (!reader.connect(callBack)) {
			reader = null;
		}
		return reader;
	}

	@Override
	public boolean disconnect(R2000Reader reader) {
		if (null == reader) {
			return false;
		}
		return reader.disconnect();
	}

	@Override
	public String version(R2000Reader reader) {
		if (null == reader) {
			return null;
		}
		return reader.version();
	}

	@Override
	@Deprecated
	public boolean invOnce(R2000Reader reader) {
		if (null == reader) {
			return false;
		}
		return reader.invOnce();
	}

	@Override
	@Deprecated
	public boolean beginInv(R2000Reader reader) {
		if (null == reader) {
			return false;
		}
		return reader.beginInv();
	}

	@Override
	@Deprecated
	public boolean stopInv(R2000Reader reader) {
		if (null == reader) {
			return false;
		}
		return reader.stopInv();
	}

	@Override
	public AntStruct getAntenna(R2000Reader reader) {
		if (null == reader) {
			return null;
		}
		if(reader.channel < 1){
			version(reader);
		}
		if(reader.channel < 1){
			return null;
		}
		AntStruct struct = new AntStruct(reader.channel);
		byte[] buffer = reader.getAntenna();
		if(null == buffer){
			return null;
		}
		if (struct.state == 4) {
			return reader.antenna4Channel(buffer,reader.channel);
		} else {
			return reader.antenna32Channel(buffer,reader.channel);
		}
	}

	@Override
	public boolean setAntenna(R2000Reader reader, AntStruct ant) {
		if (null == reader) {
			return false;
		}
		return reader.setAntenna(ant);
	}

	@Override
	public boolean writeTagData(R2000Reader reader, int bank, int begin, int length,String data, byte[] password) {
		if (null == reader) {
			return false;
		}
		if (bank < 0 || begin < 0 || length < 0) {
			try {
				throw new RFIDException("bank/begin/length Must be a positive integer??");
			} catch (RFIDException e) {
				e.printStackTrace();
			}
		}
		if (bank > 3) {
			try {
				throw new RFIDException("bank cannot be anything but 0-3");
			} catch (RFIDException e) {
				e.printStackTrace();
			}
		}
		if (bank == 1 && (begin + length > 8 || begin < 2)) {
			try {
				throw new RFIDException("When writing the contents of the EPC region, begin must start at 2, and begin(write the address in the region) + length(the length to be written) has a value of no more than 8.Check the input parameter values");
			} catch (RFIDException e) {
				e.printStackTrace();
			}
		}
		if (bank == 0 && (begin + length > 4)) {
			try {
				throw new RFIDException("When writing the tenure contents, begin(the address in the write field)+length(the length to be written) does not exceed 4.Check the input parameter values");
			} catch (RFIDException e) {
				e.printStackTrace();
			}
		}
		if (data.length() != 4 * length) {
			try {
				throw new RFIDException("The length of data must be length * 4");
			} catch (RFIDException e) {
				e.printStackTrace();
			}
		}
		if (password == null || "".equals(password)) {
			for (int i = 0; i < 4; ++i) {
				password[0] = (byte) 0;
			}
		}
		return reader.writeTagData(bank, begin, length, data, password);
	}

	@Override
	public boolean lockTag(R2000Reader reader, byte locktType, byte lockBank,byte[] password) {
		if (null == reader) {
			return false;
		}
		return reader.lockTag(locktType, lockBank, password);
	}

	@Override
	public int getBuzzer(R2000Reader reader) {
		if (null == reader) {
			return -1;
		}
		return reader.getBuzzer();
	}

	@Override
	public boolean setBuzzer(R2000Reader reader, byte state) {
		if (null == reader) {
			return false;
		}
		return reader.setBuzzer(state);
	}

	@Override
	public int getWorkMode(R2000Reader reader) {
		if (null == reader) {
			return -1;
		}
		return reader.getWorkMode();
	}
	
	@Override
	public boolean setWorkMode(R2000Reader reader, int mode) {
		if (null == reader) {
			return false;
		}
		return reader.setWorkMode(mode);
	}

	@Override
	public boolean setTriggerModeDelayTime(R2000Reader reader, byte trigTime) {
		if (null == reader) {
			return false;
		}
		return reader.setTriggerModeDelayTime(trigTime);
	}

	@Override
	public int getTriggerModeDelayTime(R2000Reader reader) {
		if (null == reader) {
			return -1;
		}
		return reader.getTriggerModeDelayTime();
	}

	@Override
	public Map<String,Byte> getAdjacentDiscriminant(R2000Reader reader) {
		if (null == reader) {
			return null;
		}
		return reader.getAdjacentDiscriminant();
	}

	@Override
	public boolean setAdjacentDiscriminant(R2000Reader reader, byte neighJudgeTime) {
		if (null == reader) {
			return false;
		}
		return reader.setAdjacentDiscriminant(neighJudgeTime);
	}

	@Override
	public String getDeviceNo(R2000Reader reader) {
		if (null == reader) {
			return null;
		}
		return reader.getDeviceNo();
	}

	@Override
	public boolean setDeviceNo(R2000Reader reader, int deviceNo) {
		if (null == reader) {
			return false;
		}
		return reader.setDeviceNo(deviceNo);
	}

	@Override
	public int getOutputMode(R2000Reader reader) {
		if (null == reader) {
			return -1;
		}
		return reader.getOutputMode();
	}

	@Override
	public boolean setOutputMode(R2000Reader reader, byte outputMode) {
		if (null == reader) {
			return false;
		}
		return reader.setOutputMode(outputMode);
	}

	@Override
	public boolean killTag(R2000Reader reader, byte[] accessPwd, byte[] killPwd) {
		if (null == reader) {
			return false;
		}
		return reader.killTag(accessPwd, killPwd);
	}

	@Override
	public Map<String, Boolean> getGPIO(R2000Reader reader) {
		if (null == reader) {
			return null;
		}
		return reader.getGPIO();
	}

	@Override
	public boolean setGPIO(R2000Reader reader, byte port, byte state) {
		if (null == reader) {
			return false;
		}
		return reader.setGPIO(port,state);
	}

	@Override
	public boolean setDeviceConfig(R2000Reader reader, byte[] para) {
		if (null == reader) {
			return false;
		}
		return reader.setDeviceConfig( para);
	}

	@Override
	public String readTagData(R2000Reader reader, byte bank, byte begin, byte length,byte[] password) {
		if (null == reader) {
			return null;
		}
		return reader.readTagData( bank, begin, length,password);
	}
	
	/**************2018-12-03 new add ****************/
	
	@Override
	public boolean invOnceV2(R2000Reader reader) {
		if (null == reader) {
			return false;
		}
		return reader.invOnceV2();
	}

	@Override
	public boolean beginInvV2(R2000Reader reader) {
		if (null == reader) {
			return false;
		}
		return reader.beginInvV2();
	}

	@Override
	public boolean stopInvV2(R2000Reader reader) {
		if (null == reader) {
			return false;
		}
		return reader.stopInvV2();
	}
	
	@Override
	public Map<String, Integer> getInvPatternConfig(R2000Reader reader) {
		if (null == reader) {
			return null;
		}
		return reader.getInvPatternConfig();
	}

	@Override
	public boolean setInvPatternConfig(R2000Reader reader, byte session,	byte qValue, byte tagFocus, byte abValue) {
		if (null == reader) {
			return false;
		}
		return reader.setInvPatternConfig(session,qValue, tagFocus, abValue);
	}

	@Override
	public boolean factoryDataReset(R2000Reader reader) {
		if (null == reader) {
			return false;
		}
		return reader.factoryDataReset();
	}

	@Override
	public Map<String, Boolean> getInvOutPutData(R2000Reader reader) {
		if (null == reader) {
			return null;
		}
		return reader.getInvOutPutData();
	}

	@Override
	public boolean setInvOutPutData(R2000Reader reader, byte antenna, byte rssi,byte deviceNo, byte accessDoorDirection) {
		if (null == reader) {
			return false;
		}
		return reader.setInvOutPutData(antenna, rssi,deviceNo, accessDoorDirection);
	}

	@Override
	public Map<String,Byte> getAntennaStatus(R2000Reader reader) {
		if (null == reader) {
			return null;
		}
		return reader.getAntennaStatus();
	}

	@Override
	public FrequencyPoint getFrequency(R2000Reader reader) {
		if (null == reader) {
			return null;
		}
		return reader.getFrequency();
	}

	@Override
	public boolean setFrequency(R2000Reader reader,int type,double frequencyFixed,boolean[] frequencyHopping) {
		if (null == reader) {
			return false;
		}
		return reader.setFrequency(type,frequencyFixed,frequencyHopping);
	}

	@Override
	public boolean setTriggerModeAlertTime(R2000Reader reader, byte time) {
		if (null == reader) {
			return false;
		}
		return reader.setTriggerModeAlertTime(time);
	}
}
